# -*- coding: utf-8 -*-

import unittest

from requests_html import HTMLSession

from mock.wdzj.mock_mansit import ParseManSit
from mock.wdzj.mock_wdzj import MockWDZJ
from utils.GeneratorUtils import GeneratorUtils


class MockWDZJTest(unittest.TestCase):

    def setUp(self):
        self.gen_tools = GeneratorUtils()

    def test_obtain_sign_data(self):
        wdzj = MockWDZJ('https://member.wdzj.com/member/appclub/rebateSignData?minid=0&pagesize=15')
        wdzj.obtain_sign_data()

    def test_parse_qsbk_text(self):
        wdzj = MockWDZJ('baidu')
        wdzj.parse_qsbk_text()

    def test_gen_mobile_sell_data(self):
        print('hello %s, %d' % ('Allen', 12))
        gen = GeneratorUtils()
        ret = gen.gen_mobile_sell_data(12, 'Apple', 'Mi')
        print(ret)

    def test_req_html_tuan(self):
        url = 'http://zi.nanrenvip.life/s1s1s1/'
        html_session = HTMLSession()
        get_res = html_session.get(url)

        gen_tools = GeneratorUtils()
        content_file_name = '../../context_{}.html'.format(gen_tools.gen_random_str(6))
        print('context file name: %s' % content_file_name)
        text_file_name = '../../text_{}.html'.format(gen_tools.gen_random_str(6))
        print('text file name: %s' % text_file_name)

        try:
            get_res.encoding = 'utf-8'
            with open(content_file_name, 'w', encoding='utf-8') as fn:
                context = str(get_res.content, encoding='utf-8')
                fn.write(context)

            with open(text_file_name, 'w', encoding='utf-8') as fn:
                fn.write(get_res.text)
        except IOError:
            print('write file Error')

    def test_req_actress_html(self):
        # url = 'http://ng.nanrenvip.life/nvyouku/'
        url = 'http://ng.nanrenvip.life/nvyouku/qiaobenyoucai.html'
        session = HTMLSession()
        res = session.get(url)
        res.encoding = 'utf-8'

        actress_file = '../../qbyc_{}.html'.format(self.gen_tools.gen_random_str(6))
        print('actress file path: ', actress_file)

        try:
            with open(actress_file, 'w', encoding='utf-8') as fn:
                fn.write(res.text)
            print('completed parse data write file')
        except IOError:
            print('write file error')

    def test_download_img_file(self):
        url = 'http://nanrenvip.la/d/file/2018/08/b819753fab35c938f9466fd3f8008bee.jpg'
        self.gen_tools.download_img(img_url=url, img_name=self.gen_tools.gen_random_str(4) + '.jpg')

    def test_parse_actress_date(self):
        actress_url = [
            'http://ng.nanrenvip.life/nvyouku/ssyouya.html',
            # 'http://ng.nanrenvip.life/nvyouku/qiaobenyoucai.html'
        ]

        parse = ParseManSit([], actress_url)
        ret = parse.parse_actress_date(*actress_url)
        print(ret)
